<template>
    <div>
        <div class="ccui-multifunctional-table-wrapper">
            <el-table
                :data="showTableData"
                class="ccui-form-child-table"
                border
                show-summary
                :summary-method="getSummaries"
                @select="listSelectTable"
                @select-all="listSelectTable"
                max-height="9999"
                style="margin-bottom: 30px; width: 100%"
            >
                <el-table-column
                    show-overflow-tooltip
                    label="序号"
                    type="index"
                    align="center"
                    width="55"
                    fixed
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="acceptCode"
                    label="验收单编号"
                    width="130"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="materialCode"
                    width="110"
                    label="设备物资编码"
                    fixed
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="materialName"
                    width="300"
                    label="设备物资名称"
                    fixed
                    :formatter="handleFormatter"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="materialState"
                    width="130"
                    label="设备物资说明"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="measureUnit"
                    label="计量单位"
                    width="90"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="acceptTime"
                    width="100"
                    label="验收日期"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="unTransportCount"
                    label="剩余可结算数量"
                    width="130"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="settlementCount"
                    width="100"
                    label="结算数量"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="taxRatePercent"
                    label="税率"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="unitPriceWithTax"
                    width="130"
                    label="含税单价"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="moneyWithTax"
                    width="130"
                    label="含税金额"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="unitPriceWithoutTax"
                    label="不含税单价"
                    min-width="150"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="moneyWithoutTax"
                    label="不含税金额"
                    min-width="150"
                />
                
                <el-table-column
                    show-overflow-tooltip
                    prop="brand"
                    width="130"
                    label="生产厂家/品牌"
                />
    
<!--                <el-table-column-->
<!--                    show-overflow-tooltip-->
<!--                    prop="remarks"-->
<!--                    label="备注"-->
<!--                    width="100"-->
<!--                />-->
            </el-table>
        </div>
        
        <chooseMaterials
            ref="materialsFormVisible"
            @handleSelect="handleSelect"
            :chooseMaterialsBtn="chooseMaterialsBtn"
        />
    </div>
</template>

<script>
import chooseMaterials from "@/basa/components/public/chooseMaterials";
import {formatNumber} from "@/basa/utils";
import jsList from "../js";
import BigNumber from "bignumber.js";

export default {
    name: "financeInfoCheck",
    components: {chooseMaterials},
    props: {
        settlementList: {
            type: Array
        },
        settlementForm: {
            type: Object
        }
    },
    filters: {
        handleMoney(val) {
            return formatNumber(val, 2);
        }
    },
    data() {
        return {
            showTableData: [],
            jsList,
            listTableData: [],
            materialVisible: false,
            chooseMaterialsBtn: 0 // 点击选择物资按钮时为true
        };
    },
    created() {
    },
    mounted() {
    },
    activited() {
    },
    update() {
    },
    methods: {
        handleFormatter(row) {
            if (
                row.materialName == "贴息" ||
                row.materialName == "价格追涨" ||
                row.materialName == "价格追跌" ||
                row.materialName == "其他" ||
                (row.categoryCode &&
                    row.categoryCode.substr(0, 4) ==
                    ("1305" || "1306" || "1307" || "1308" || "1309")) ||
                row.categoryCode.substr(0, 6) == "990103"
            ) {
                let arr = [
                    "acceptCount",
                    "settlementCount",
                    "taxRate",
                    "unitPriceWithTax",
                    "unitPriceWithoutTax"
                ];
                arr.forEach(e => {
                    row[e] = "";
                });
            }
            return row.materialName;
        },
        // 采购计划选择确认
        planConfirm(data) {
            console.log("采购计划选中行==>>", data);
        },
        // 采购方案选择确认
        programmeConfirm(data) {
            console.log("采购方案选中行==>>", data);
        },
        // 选择材料
        handleChooseMaterials() {
            // 打开新增物资清单页面
            this.$refs.materialsFormVisible.dialogFormVisible = true;
            this.chooseMaterialsBtn++;
        },
        // 采购清单选中数据
        listSelectTable(val) {
            this.listTableData = val;
        },
        handleSelect(row) {
        },
        // 合计
        getSummaries(param) {
            const {columns, data} = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = "合计";
                    return;
                }
                const values = data.map(item => Number(item[column.property]));
                if (!values.every(value => isNaN(value))) {
                    sums[index] = values.reduce((prev, curr, rowIndex) => {
                        const value = Number(curr);
                        if (!isNaN(value)) {
                            if (column.label == "结算数量") {
                                // if (values.isMerge == 0 && values.isMainInventory == 1) {
                                //     console.log(prev);
                                // }
                                // return new BigNumber(+prev).plus(+curr).toString();
                                
                                if (
                                    data[rowIndex].isMerge == 1 &&
                                    data[rowIndex].isMainInventory == 0
                                ) {
                                    return prev;
                                }
                                return new BigNumber(+prev).plus(+curr).toString();
                            } else if (
                                column.label == "含税金额" ||
                                column.label == "不含税金额"
                            ) {
                                if (
                                    data[rowIndex].isMerge == 1 &&
                                    data[rowIndex].isMainInventory == 0
                                ) {
                                    return prev;
                                }
                                return new BigNumber(+prev).plus(+curr).toString();
                            }
                        } else {
                            return (prev = " ");
                        }
                    }, 0);
                    // if (column.label == "验收数量") {
                    //   sums[index] = this.calculateFilter(sums[index]);
                    // }
                    if (column.label == "结算数量") {
                        sums[index] = (+sums[index]).toFixed(3);
                    }
                    // if (column.label == "含税单价(元)") {
                    //   sums[index] = this.calculateFilter(sums[index]);
                    // }
                    if (column.label == "含税金额") {
                        sums[index] = (+sums[index]).toFixed(2);
                    }
                } else {
                    sums[index] = "";
                }
            });
            return sums;
        },
        // 金额四舍五入 8位小数
        calculateFilter(data) {
            if (data || data == 0) {
                if ((data + "").indexOf(".") != -1) {
                    let newData = (data + "").substring(0, (data + "").indexOf(".") + 9);
                    return newData;
                } else {
                    return data;
                }
            } else {
                return "";
            }
        },
        // 金额四舍五入
        numberFilter(data) {
            if (data || data == 0) {
                if ((data + "").indexOf(".") != -1) {
                    let newData = (data + "").substring(0, (data + "").indexOf(".") + 4);
                    return newData;
                } else {
                    return data;
                }
            } else {
                return "";
            }
        },
        
        tableRowClassName(data) {
            if (data.row.isMerge == 1 && data.row.isMainInventory == 0) {
                return "hidden-row";
            }
        },
        handleTable(approvalStatus) {
            if (this.settlementList.length) {
                let yunshu = [], // 运输类
                    hebingqian = [], // 没有合并的数据
                    hebinghou = [], // 合并后隐藏的数据
                    hebingMerge = [], // 合并后生成的数据
                    qita = []; // 其他类数据
                this.settlementList.map(e => {
                    if (e.isCategories && e.isCategories == 1) {
                        if (approvalStatus == 3) {
                            if (e.moneyWithTax != 0) yunshu.push(e);
                        } else {
                            yunshu.push(e);
                        }
                    } else if (e.isMerge == 0 && e.isMainInventory == 0 && e.isOtherCost == 0) {
                        hebingqian.push(e);
                    } else if (e.isMerge == 1 && e.isMainInventory == 0 && e.isOtherCost == 0) {
                        hebinghou.push(e);
                    } else if (e.isMerge == 1 && e.isMainInventory == 1 && e.isOtherCost == 0) {
                        hebingMerge.push(e);
                    } else if (e.isOtherCost && e.isOtherCost == 1) {
                        qita.push(e);
                    }
                });
                this.showTableData = [...hebingqian, ...hebingMerge, ...yunshu, ...qita];
            } else {
                this.showTableData = [];
            }
        },
    },
    filter: {},
    computed: {},
    watch: {}
};
</script>

<style lang="scss" scoped>
/deep/ .ccui-multifunctional-table-wrapper {
    .el-table td,
    .el-table th.is-leaf {
        border-right: 1px solid #edf2fc;
    }
}

.ml {
    margin-left: 10px;
}

.operation-group {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin-bottom: 20px;
}

/deep/
.ccui-multifunctional-table-wrapper
.el-table
tbody
tr
td:first-child
.cell {
    border-right: 1px solid #f1f1f1;
}

.deleteLine {
    padding: 0 20px;
    height: 30px;
    background: #f7f9ff;
    border: 1px solid #0089ef;
    border-radius: 4px;
    position: relative;
    color: #0089ef;
    margin-left: 10px;
}

/deep/ .hidden-row {
    display: none;
}

///deep/ .el-table__body-wrapper {
//  z-index: 1;
//}

/deep/ .el-table__fixed-header-wrapper {
    th {
        pointer-events: auto;
    }
}

/deep/ .el-table__fixed,
/deep/ .el-table__fixed-right {
    pointer-events: none;
    
    td {
        pointer-events: auto;
    }
}
</style>
